package com.xzs.kit.base.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : Victor.Xiao
 * @date : 2017/12/02 15:31
 * @description :
 */
public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 总记录数
     */
    private long total;
    /**
     * 结果集
     */
    private List<T> list;
    /**
     * 第几页
     */
    private int pageNo=1;
    /**
     * 每页记录数
     */
    private int pageSize=20;
    /**
     * 总页数
     */
    private int pages;
    /**
     * 当前页的数量<=pageSize
     */
    private int size;

    @JSONField(serialize=false)
    /**
     * 查询参数
     */
    private Map<String,Object> params;
    @JSONField(serialize=false)
    /**
     * 排序
     */
    private String orderBy;
    @JSONField(serialize=false)
    /**
     * 是否查询结果数量
     */
    private Boolean selectCountFlag=true;

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public Boolean getSelectCountFlag() {
        return selectCountFlag;
    }

    public void setSelectCountFlag(Boolean selectCountFlag) {
        this.selectCountFlag = selectCountFlag;
    }

    public PageBean(){
    }
    public PageBean(List<T> list){
        setList(list);
    }
    public long getTotal() {
        return total;
    }
    public void setTotal(long total) {
        this.total = total;
    }
    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        if (list instanceof Page){
            Page<T> page = (Page<T>) list;
            this.pageNo = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
        }else{
            this.list = list;
        }
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getPages() {
        return pages;
    }
    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public void addParams(String key,Object obj) {
        if(params==null){
            params = new HashMap<String, Object>(8);
        }
        params.put(key,obj);
    }
}
